package demo2;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-07-15
 * Time: 20:52
 */
public class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    public static void BubbleSort(Comparable[] comparables){
        for(int i = 0; i < comparables.length - 1; i++){
            for(int j = 0; j < comparables.length - 1 - i; j++){
                if(comparables[j].compareTo(comparables[j + 1]) > 0){
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j + 1];
                    comparables[j + 1] = tmp;
                }
            }
        }
    }

    public static void mai1n(String[] args) {
        Student student1 = new Student("张三", 10);
        Student student2 = new Student("李四", 20);
        Student student3 = new Student("王五", 30);
        Student[] students = new Student[3];
        students[0] = student1;
        students[1] = student2;
        students[2] = student3;
        BubbleSort(students);
        System.out.println(Arrays.toString(students));
    }
}
